package com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.builder;

import android.os.Build;
import androidx.annotation.RequiresApi;
import android.text.TextUtils;


import com.cn.ZhiKeDianShang.base.GlobalApp;
import com.cn.ZhiKeDianShang.util.LogUtil;
import com.cn.ZhiKeDianShang.util.PrefShare;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.HttpUtils;


import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;


/**
 * @author Administrator
 * @time 2017/3/30 0:11
 * @des ${TODO}
 */

public class GetBuilder extends BaseBuilder {
    private static final String TAG = "GetBuilder";

    @Override
    public GetBuilder url(String url) {
        if (!TextUtils.isEmpty(url)) {
            this.url = url;
        }
        return this;
    }


    /**
     * 给所有  需要 token userid接口调添加secret,
     */
    public GetBuilder addSecret() {
        String token = PrefShare.getInstance(GlobalApp.getAppContext()).getString("token");
        String userId = PrefShare.getInstance(GlobalApp.getAppContext()).getString("userId");
        params.put("token", token);
        params.put("userid", userId);
        return this;
    }


    @Override
    public GetBuilder tag(Object tag) {
        this.tag = tag;
        return this;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public GetCall build() {
        url = appenParams();
        build = new Request.Builder().url(url).build();
        GetCall call = new GetCall();
        LogUtil.i(HttpUtils.TAG, "get网络请求参数：" + url);
        // 如果tag不为空，进入队列管理
        if (httpUtils != null && tag != null) {
            Map<Object, List<Object>> mCalls = httpUtils.getCalls();
            if (tag != null) {
                List<Object> list = mCalls.get(tag);
                if (list == null) {
                    list = new ArrayList<>();
                    mCalls.put(tag, list);
                }
                list.add(call);
            }
        }
        return call;
    }


    /**
     * 请求参数没有加密时候拼接
     *
     * @return
     */

//    private String appenParams() {
//        StringBuffer sb = new StringBuffer();
//        sb.append(url);
//        if (params != null && !params.isEmpty()) {
//            sb.append("?");
//            for (String key : params.keySet()) {
//                sb.append(key).append("=").append(params.get(key)).append("&");
//            }
//            sb = sb.deleteCharAt(sb.length() - 1); // 去掉后面的&
//        }
//
//        return sb.toString();
//    }


    //加密的请求
    @RequiresApi(api = Build.VERSION_CODES.O)
    private String appenParams() {

        StringBuffer sb = new StringBuffer();
        sb.append(url);
        if (params != null && !params.isEmpty()) {
            sb.append("?");
            for (String key : params.keySet()) {
                sb.append(key).append("=").append(params.get(key)).append("&");
            }
            sb = sb.deleteCharAt(sb.length() - 1); // 去掉后面的&
        }

        return sb.toString();

    }

    @Override
    public GetBuilder params(String k, String v) {
        try {
            // url安全，部分字符不能直接放进url, 要改成百分号开头%的，
            v = URLEncoder.encode(v, "UTF-8");
        } catch (Exception e) {
            // 不可到达，UTF-8不可能不支持，
            e.printStackTrace();
        }
        // this.url = this.url+k+"="+v;
        if (params == null) {
            params = new LinkedHashMap<>();
        }
        params.put(k, v);
        return this;
    }

    public GetBuilder params(Map<String, String> params) {
        for (String key : params.keySet()) {
            params(key, params.get(key));
        }

        return this;
    }

    public Object getTag() {
        return this.tag;
    }

    public GetBuilder(HttpUtils httpUtils) {
        this.httpUtils = httpUtils;
    }

    private HttpUtils httpUtils;

    public class GetCall extends BaseCall {
        private Call call;

        @Override
        public Call execute(Callback callback) {
            Call call = super.execute(callback);
            this.call = call;
            return call;
        }

        public Call getCall() {
            return call;
        }
    }
}
